<?php
/**
 * Created by PhpStorm.
 * User: tengwei
 * Date: 2018/11/22
 * Time: 9:56 AM
 */

namespace common\sources\read\businesscash;

use common\models\craftsman\BusinessCash;

class BusinessCashAccess implements IBusinessCashAccess {
	public function getBusinessCashRecord($cash_record_id) {
		return BusinessCash::find()
			->select("id,bus_id,serial_num,money,arrival_money,arrival_money,account_num,username,cash_status")
			->where("id = " . $cash_record_id)
			->asArray()
			->one();
	}

	public function CashAuditList($data) {
		$query = BusinessCash::find()
			->alias('a')
			->select('a.kq_serial_number,a.end_time,a.auditor_time,,a.id,a.bus_id,a.serial_num,a.money,a.arrival_money,a.cash_status,a.remark,a.create_time,a.account_num,
            b.real_name,b.bankname,b.pro_name,b.city_name,b.bankbranch,
            c.role,c.phone')
			->leftJoin('bussiness_bank_account b', 'a.account_num = b.cardnum')
			->leftJoin('buyuser c', 'a.bus_id = c.id');
		if (!empty($data['phone'])) {
			$query = $query->andWhere(['c.phone' => $data['phone']]);
		}
		if (!empty($data['status'])) {
			$query = $query->andWhere(['a.cash_status' => $data['status']]);
		}
		if (!empty($data['cash_num'])) {
			$query = $query->andWhere(['a.serial_num' => $data['cash_num']]);
		}
		if (1 == $data['time_type']) {//1-提现时间 2-审核时间 3-完成时间
			if (!empty($data['begin_time'])) {
				$query = $query->andWhere(['>', 'a.create_time', $data['begin_time']]);
			}
			if (!empty($data['end_time'])) {
				$query = $query->andWhere(['<', 'a.create_time', $data['end_time']]);
			}
		}
		if (2 == $data['time_type']) {//1-提现时间 2-审核时间 3-完成时间
			if (!empty($data['begin_time'])) {
				$query = $query->andWhere(['>', 'a.auditor_time', $data['begin_time']]);
			}
			if (!empty($data['end_time'])) {
				$query = $query->andWhere(['<', 'a.auditor_time', $data['end_time']]);
			}
		}
		if (3 == $data['time_type']) {//1-提现时间 2-审核时间 3-完成时间
			if (!empty($data['begin_time'])) {
				$query = $query->andWhere(['>', 'a.end_time', $data['begin_time']]);
			}
			if (!empty($data['end_time'])) {
				$query = $query->andWhere(['<', 'a.end_time', $data['end_time']]);
			}
		}
		$list = $query->asArray()->all();
		return $list;
	}

	public function CashDetail($data) {
		$result = BusinessCash::find()
			->alias('a')
			->select('a.id,a.kq_serial_number,a.bus_id as buy_id,a.serial_num as cash_num,a.money,a.arrival_money as cash_money,a.cash_status as status,a.remark,a.create_time,a.ww_verify_img,
            b.real_name,b.bankname,b.pro_name,b.city_name,b.bankbranch,b.cardnum,a.cash_status,a.auditor_user,a.end_user,a.end_time,
            c.role,c.phone,
            d.freezing_golds as freezing')
			->leftJoin('bussiness_bank_account b', 'a.account_num = b.cardnum')
			->leftJoin('buyuser c', 'a.bus_id = c.id')
			->leftJoin('business_account d', 'a.bus_id = d.bus_id')
			->where(['a.serial_num' => $data['cashNo']])
			->asArray()
			->one();
		return $result;
	}

	public function getBusCashAuditCount($data)
    {
        return BusinessCash::find()
                ->where($data)
                ->count();
    }

	public function ModifyRemark($data) {
		$cash = BusinessCash::findOne(['id' => $data['id']]);
		if ($cash) {
			$cash->remark = $data['remark'];
			if ($cash->save()) {
				return true;
			}
		}
		return false;
	}

	public function AuditCash($data) {
		$cash = BusinessCash::findOne(['id' => $data['id']]);
		if ($cash) {
			$cash->cash_status = $data['status'];
			if ($cash->save()) {
				return true;
			}
		}
		return false;
	}
}